package com.itqf.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
 * projectName: shop_manage
 *
 * @author: MaTianWei
 * time: 2021/7/13 17:25
 * description:
 */

public class ReflectionDemo {
    public static void main(String[] args) {
        //1·获取类对象的模板
        Class<AdminRoleController> clazz = AdminRoleController.class;
        //获取注解：类 class对象获取 方法 method对象获取 属性 fieid对象获取
        //获取注解
        RequestMapping annotation = clazz.getAnnotation(RequestMapping.class);
        //获取注解的属性 注解属性名
        String base = annotation.value()[0];
        System.out.println("base="+base);
        //获取所有的方法
        Method[] methods = clazz.getMethods();
        Set<String> permissions = new HashSet<>();
        for(Method method : methods){
            boolean get = method.isAnnotationPresent(GetMapping.class);
            boolean post = method.isAnnotationPresent(PostMapping.class);
            if(get||post){
                String permission = "";

                if (get){
                    permission +="GET";
                    GetMapping getMapping = method.getAnnotation(GetMapping.class);
                    String getUrl = getMapping.value()[0];
                    permission = permission+base+getUrl;
                }else{
                    permission +="POST";
                    PostMapping postMapping = method.getAnnotation(PostMapping.class);
                    String postUrl = postMapping.value()[0];
                    permission = permission +base+postUrl;
                }
                permissions.add(permission);
            }
        }
        System.out.println("permissions="+permissions);
    }
}
